package com.wmcom.wm.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.wmcom.wm.common.R;
import com.wmcom.wm.dto.OrdersDto;
import com.wmcom.wm.entity.OrderDetail;
import com.wmcom.wm.entity.Orders;
import com.wmcom.wm.service.OrderDetailService;
import com.wmcom.wm.service.OrdersService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单表
 */
@RestController
@RequestMapping("/order")
public class OrdersController {
    @Resource
    private OrdersService ordersService;
    @Resource
    private OrderDetailService orderDetailService;

    /**
     * 用户下单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
         ordersService.submit(orders);
        return R.success("ok");
    }
    /**
     *
     * 订单显示
     */
    @GetMapping("/userPage")
    public R<Page<OrdersDto>> userPage(int page, int pageSize, HttpSession session){
           Page<Orders>ordersPage=new Page<>(page,pageSize);
           Page<OrdersDto> ordersDto=new Page<>();
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Orders::getUserId, session.getAttribute("user"));
         ordersService.page(ordersPage, wrapper);
        //    对象拷贝
        BeanUtils.copyProperties(ordersPage,ordersDto,"records");
        List<Orders> ordersList=ordersPage.getRecords();
        List<OrdersDto>ordersDtoList=null;
        ordersDtoList =ordersList.stream().map((item)->{
            OrdersDto ordersDto1=new OrdersDto();
            BeanUtils.copyProperties(item,ordersDto1);
        //    订单明细id
            long id = item.getId();
             LambdaQueryWrapper<OrderDetail>wrapperQueryWrapper = new LambdaQueryWrapper<>();
            wrapperQueryWrapper.eq(OrderDetail::getOrderId,id);
            List<OrderDetail> orderDetailList = orderDetailService.list(wrapperQueryWrapper);
            if (orderDetailList!=null) {
                ordersDto1.setOrderDetails(orderDetailList);
          }
           return ordersDto1;
        }).collect(Collectors.toList());
        ordersDto.setRecords(ordersDtoList);
          return R.success(ordersDto);
    }
    /**
     * 管理订单明细
     */
@GetMapping("/page")
    public R<Page<Orders>> page(int page,int pageSize, String number){
    Page<Orders> page1=new Page<>(page,pageSize);
    LambdaQueryWrapper<Orders> wrapper=new LambdaQueryWrapper<>();
    if (number != null) {
        wrapper.eq(Orders::getPhone,number);
    }
    ordersService.page(page1,wrapper);
    return R.success(page1);
}
    /**
     *管理订单派送完成情况
     */
    @PutMapping
    public R<Orders> put(@RequestBody Orders order){
         LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
         wrapper.eq(Orders::getId,order.getId());
        Orders orders = ordersService.getOne(wrapper);
        orders.setStatus(order.getStatus());
        ordersService.updateById(orders);
        return R.success(orders);
    }
}
